﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VCC.Model;
using VCC.Lib.Common;
using System.Linq.Dynamic;
using VCC.Site.ViewModels;
using VCC.Site.Properties;
namespace VCC.Site.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /Menu/
        ChoVietDataContext eDataContext = new ChoVietDataContext();
        /// <summary>
        /// Only apply for menu has leve 3
        /// </summary>
        /// <returns></returns>
        /*
        public ActionResult GetLeft()
        {
            var url = Request.Url.PathAndQuery;
            var m = db.Menus.Where(e => (e.PubYN == true) && (e.NavigateUrl == url) && (e.TypeID == MenuListType.Top)).SingleOrDefault();
            if (m != null && m.MenuMaps.Count > 0)
            {
                var l = db.Menus.Where(e =>(e.PubYN == true)&&( e.ParentID == m.MenuMaps[0].LinkToID)).OrderBy(string.Format("{0} {1}", "Priority", "asc")).ToList();
                return View(l);
            }
            else {
                var ml = db.Menus.Where(e => (e.PubYN == true) && (e.NavigateUrl == url) && (e.TypeID == MenuListType.Left)).SingleOrDefault();
                
                var parentid = ml.ParentMenu == null ? ml.ID : (ml.ParentMenu.ParentMenu == null ? ml.ParentMenu.ID : ml.ParentMenu.ParentID);
                var l = db.Menus.Where(e => (e.PubYN == true) && (e.ParentID == parentid)) 
                    .OrderBy(string.Format("{0} {1}", "Priority", "asc")).ToList();
                return View(l); 
            }           
        }
         */ 
        public ActionResult MenuTop()
        {
            MenuTopViewModel model = new MenuTopViewModel();
            try 
            {
                if (eDataContext.Connection.State == System.Data.ConnectionState.Closed)
                {
                    eDataContext.Connection.Open();
                }
                model.Menus = eDataContext.Menus.Where(e => e.PubYN == true && e.ParentID == null).OrderBy(e => e.Priority).ToList();
               // model.LanguageID = Utils.ParseInt(Session["LanguageID"].ToString());
                //List<Menu> menus = eDataContext.Menus.Where(e => e.PubYN == true).ToList();
                //List<Menu> ParentMenus = menus.Where(e => e.ParentID == null).OrderBy(e => e.Priority).ToList();
                //List<Menu> FinallMenus = new List<Menu>();
                //foreach (Menu item in ParentMenus)
                //{
                //    FinallMenus.Add(item);
                //    List<Menu> childen = menus.Where(e => e.ParentID == item.ID).OrderBy(e => e.Priority).ToList();
                //    if (childen != null && childen.Count() > 0)
                //    {
                //        FinallMenus.AddRange(childen);
                //    }
                //}
                //model.Menus = FinallMenus;
                return View(model);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                eDataContext.Dispose();
                return Redirect(Settings.Default.Domain + "Upgrade.aspx");
            }
        }

        public ActionResult GetBottom()
        {
            try
            {
                MenuBottomViewModel model = new MenuBottomViewModel();
                model.LanguageID = Utils.ParseInt(Session["LanguageID"].ToString());
                model.Menus = eDataContext.Menus.Where(e => (e.PubYN == true) && (e.ParentID == null) && (e.TypeID == MenuListType.Bottom)).OrderBy(e => e.Priority).ToList();
                return View(model);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }
    }
}